<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SwitchModel
 *
 * @author Admin
 */
class Application_Model_SwitchModel extends Zend_Db_Table_Abstract {

    protected $_name = 'switch_mgmt';
    protected $_fields = 'id,name,cabinet,planning_percent_done,street_percent_done,street_status,house_status,status,contract_price,installation_status,ritningar,skarvschema,mangdning,budget,slutavstamning,tomtprojektering,anmarkning_tomt,gravd_datum,gravd_av,gravd_meter,gravd_timmar,kopplad,vagschakt_projekterad,vagschakt_avropad,vagschakt_klar,odf,port,datum_patch';
    public $adapter;

    function init() {
        $this->adapter = $this->getAdapter();
    }

    function getAllSwitches($request) {
        $select = $this->adapter->select()
                ->from($this->_name, array('id', 'name', 'planning_percent_done', 'cabinet', 'status','ritningar','skarvschema','mangdning','budget','slutavstamning','tomtprojektering','anmarkning_tomt','gravd_datum','gravd_av','gravd_meter','gravd_timmar','kopplad','vagschakt_projekterad','vagschakt_avropad','vagschakt_klar','odf','port','datum_patch'))
                ->joinLeft('cabinet_mgmt', "cabinet_mgmt.id = cabinet", array('name as cabinet_name'));
        $dbSelectPaginator = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($dbSelectPaginator);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        $paginator->setPageRange(5);
        $paginator->setItemCountPerPage(5);
        return $paginator;
    }

    function save($data) {
        if (isset($data['id'])) {
            return $this->update($data, "id = $data[id]");
        } else {
            return $this->insert($data);
        }
    }

    function getSwitch($field, $value) {
        return $this->adapter->fetchRow("select $this->_fields from $this->_name where $field = '$value'");
    }

    function getAllCompletedSwitchesForMarketing($request) {
        $select = $this->adapter->select()
                ->from($this->_name, array('id', 'name', 'cabinet', 'status', 'contract_price'))
                ->joinLeft('cabinet_mgmt', "cabinet_mgmt.id = cabinet", array('name as cabinet_name'))
                ->WHERE("switch_mgmt.marketing_status = 'Open' AND switch_mgmt.planning_percent_done = 100")
                ->order("id DESC");
        $dbSelectPaginator = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($dbSelectPaginator);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        $paginator->setPageRange(5);
        $paginator->setItemCountPerPage(5);
        return $paginator;
    }

    function getAllClosedSwitchesForStreet($request, $cabinetId) {
        $select = $this->adapter->select()
                ->from($this->_name, array('id', 'name', 'cabinet', 'street_percent_done', 'street_status', 'status', 'contract_price','tomtprojektering','anmarkning_tomt','gravd_datum','gravd_av','gravd_meter','gravd_timmar','kopplad','vagschakt_projekterad','vagschakt_avropad','vagschakt_klar','odf','port','datum_patch'))
                ->joinLeft('cabinet_mgmt', "cabinet_mgmt.id = cabinet", array('name as cabinet_name'))
                ->WHERE("switch_mgmt.marketing_status = '0'");
        if ($cabinetId) {
            $select->where("cabinet = '$cabinetId'");
        }
        $dbSelectPaginator = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($dbSelectPaginator);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        $paginator->setPageRange(5);
        $paginator->setItemCountPerPage(5);
        return $paginator;
    }

    function getAllUserAssignedSwitchesForMarketing($request, $userId) {
        $select = $this->adapter->select()
                ->from($this->_name, array('id', 'name', 'cabinet', 'status', 'contract_price','tomtprojektering','anmarkning_tomt','gravd_datum','gravd_av','gravd_meter','gravd_timmar','kopplad','vagschakt_projekterad','vagschakt_avropad','vagschakt_klar','odf','port','datum_patch'))
                ->joinInner('switch_users', "switch_users.switch_id = switch_mgmt.id", array())
                ->joinLeft('cabinet_mgmt', "cabinet_mgmt.id = switch_mgmt.cabinet", array('name as cabinet_name'))
                ->WHERE("switch_mgmt.marketing_status = 'Open' AND switch_mgmt.planning_percent_done = 100 AND switch_users.user_id='{$userId}'");
        $dbSelectPaginator = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($dbSelectPaginator);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        $paginator->setPageRange(5);
        $paginator->setItemCountPerPage(5);
        return $paginator;
    }

    function getAllUserAssignedSwitchesForStreet($request, $userId) {
        $select = $this->adapter->select()
                ->from($this->_name, array('id', 'name', 'cabinet', 'street_percent_done', 'street_status', 'status', 'contract_price','tomtprojektering','anmarkning_tomt','gravd_datum','gravd_av','gravd_meter','gravd_timmar','kopplad','vagschakt_projekterad','vagschakt_avropad','vagschakt_klar','odf','port','datum_patch'))
                ->joinInner('street_team', "street_team.switch_id = switch_mgmt.id", array())
                ->joinLeft('cabinet_mgmt', "cabinet_mgmt.id = cabinet", array('name as cabinet_name'))
                ->WHERE("switch_mgmt.marketing_status = '0' and street_team.user_id = '$userId'");

        $dbSelectPaginator = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($dbSelectPaginator);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        $paginator->setPageRange(5);
        $paginator->setItemCountPerPage(5);
        return $paginator;
    }

    function getAllClosedSwitchesForHouse($request, $cabinetId) {
        $select = $this->adapter->select()
                ->from($this->_name, array('id', 'name', 'cabinet', 'street_percent_done', 'house_status', 'status', 'contract_price','tomtprojektering','anmarkning_tomt','gravd_datum','gravd_av','gravd_meter','gravd_timmar','kopplad','vagschakt_projekterad','vagschakt_avropad','vagschakt_klar','odf','port','datum_patch'))
                ->joinLeft('cabinet_mgmt', "cabinet_mgmt.id = cabinet", array('name as cabinet_name'))
                ->WHERE("switch_mgmt.street_status = '0'");
        if ($cabinetId) {
            $select->where("cabinet = '$cabinetId'");
        }
        $dbSelectPaginator = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($dbSelectPaginator);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        $paginator->setPageRange(5);
        $paginator->setItemCountPerPage(5);
        return $paginator;
    }

    function getSwitchesProgeressForEconomy($request, $cabinetId) {
        $select = $this->adapter->select()
                ->from($this->_name, array('id as switch_id', 'name as switch_name', 'planning_percent_done as progress', 'marketing_status', 'installation_status', 'street_status', 'house_status','tomtprojektering','anmarkning_tomt','gravd_datum','gravd_av','gravd_meter','gravd_timmar','kopplad','vagschakt_projekterad','vagschakt_avropad','vagschakt_klar','odf','port','datum_patch'))
                ->WHERE("switch_mgmt.cabinet = '$cabinetId'");
        $dbSelectPaginator = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($dbSelectPaginator);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        $paginator->setPageRange(5);
        $paginator->setItemCountPerPage(5);
        return $paginator;
    }

    function getAllUserAssignedSwitchesForHouse($request, $userId) {
        $select = $this->adapter->select()
                ->from($this->_name, array('id', 'name', 'cabinet', 'street_percent_done', 'house_status', 'status', 'contract_price','tomtprojektering','anmarkning_tomt','gravd_datum','gravd_av','gravd_meter','gravd_timmar','kopplad','vagschakt_projekterad','vagschakt_avropad','vagschakt_klar','odf','port','datum_patch'))
                ->joinInner('house_team', "house_team.switch_id = switch_mgmt.id", array())
                ->joinLeft('cabinet_mgmt', "cabinet_mgmt.id = cabinet", array('name as cabinet_name'))
                ->WHERE("switch_mgmt.street_status = '0' and house_team.user_id = '$userId'");

        $dbSelectPaginator = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($dbSelectPaginator);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        $paginator->setPageRange(5);
        $paginator->setItemCountPerPage(5);
        return $paginator;
    }

    function getAllClosedSwitchesForInstallation($request, $cabinetId) {
        $select = $this->adapter->select()
                ->from($this->_name, array('id', 'name', 'cabinet', 'street_percent_done', 'house_status', 'status', 'contract_price','installation_status','tomtprojektering','anmarkning_tomt','gravd_datum','gravd_av','gravd_meter','gravd_timmar','kopplad','vagschakt_projekterad','vagschakt_avropad','vagschakt_klar','odf','port','datum_patch'))
                ->joinLeft('cabinet_mgmt', "cabinet_mgmt.id = cabinet", array('name as cabinet_name'))
                ->WHERE("switch_mgmt.house_status = '0'");
        if ($cabinetId) {
            $select->where("cabinet = '$cabinetId'");
        }
        $dbSelectPaginator = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($dbSelectPaginator);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        $paginator->setPageRange(5);
        $paginator->setItemCountPerPage(5);
        return $paginator;
    }
    
    function getAllUserAssignedSwitchesForInstall($request, $userId) {
        $select = $this->adapter->select()
                ->from($this->_name, array('id', 'name', 'cabinet', 'street_percent_done', 'house_status', 'status', 'contract_price','installation_status','tomtprojektering','anmarkning_tomt','gravd_datum','gravd_av','gravd_meter','gravd_timmar','kopplad','vagschakt_projekterad','vagschakt_avropad','vagschakt_klar','odf','port','datum_patch'))
                ->joinInner('install_team', "install_team.switch_id = switch_mgmt.id", array())
                ->joinLeft('cabinet_mgmt', "cabinet_mgmt.id = cabinet", array('name as cabinet_name'))
                ->WHERE("switch_mgmt.street_status = '0' and install_team.user_id = '$userId'");

        $dbSelectPaginator = new Zend_Paginator_Adapter_DbSelect($select);
        $paginator = new Zend_Paginator($dbSelectPaginator);
        $paginator->setCurrentPageNumber($request->getParam('page'));
        $paginator->setPageRange(5);
        $paginator->setItemCountPerPage(5);
        return $paginator;
    }
}